﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="ImportListItem.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the ImportListItem type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace Collaboris.Saf.Actions.Wss.ContentMigration
{
    using System;
    using System.Collections.Generic;

    using Engine;

    using Microsoft.SharePoint;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.ContentMigration;
    using SharePoint.Wss.Lists;
    using SharePoint.Wss.Webs;

    using Utils;

    [CollaborisClass(
             Title =
                 "Imports a List Item from a CMP.",
             ReleaseStatus =
                 ReleaseStatus.Stable,
             DisableDocumentation =
                 false,
             ContactAuthorEmail =
                 "Support@Collaboris.co.uk",
             Summary =
                 "Imports a web (SPListItem) from a Content Migration Package (CMP).",
             Remarks =
                 "<p>The 'Do' method for this action uses the standard SharePoint content migration " +
                 "API to achieve the import. " +
                 "It is highly recommended that the server is running a minimum of SharePoint " +
                 "Service Pack 1.</p>" +
                 @"This action can easily be paired with <a href=""C:\Temp\safDoc\Collaboris.Saf.Actions.Wss.ContentMigration.ExportListItem.html"">Export Web</a>" +
                 " to move content between sites and/or farms.",
               ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C835}")]
    [Serializable]
    public class ImportListItem : WSSActionBase<ListItemImportSettings>
    {
        #region Fields (1)

        ContentMigrationHelper helper = new ContentMigrationHelper();

        #endregion Fields

        #region Methods (7)

        /// <summary>
        /// When called causes the list passed in to be exported to a 
        /// CMP Package to a specified file.
        /// </summary>
        /// <param name="context">
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, ListItemImportSettings entity)
        {
            this.Trace.TraceMethodStart("DoAction");

            ValidationHelper.VerifyObjectArgument(TargetWeb.Site, "context.Site");

            SPList list = SPListHelper.GetList(TargetWeb, entity.ListName);

            helper.ImportToList(entity.ImportSettings, list, context.Site.Url, TargetWeb.Url);

            this.Trace.TraceMethodEnd("DoAction");

            return ProcessStatus.Success;
        }

        /// <summary>
        /// Checks the Data entity for Validity
        /// </summary>
        /// <param name="context">The <see cref="IContext"/>context.</param>
        /// <param name="entity">The data entity to Validate</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/> signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid</returns>
        protected override bool IsValid(SafContext context, ListItemImportSettings entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.ListName, "ListItemImportSettings.ListName", ref errors);
            
            
            ValidateObjectForNull(entity.ImportSettings, "ListItemImportSettings.ImportSettings", ref errors);
            if (entity.ImportSettings != null)
            {
                ValidateStringForNull(entity.ImportSettings.BaseFileName, "ListItemImportSettings.ImportSettings.BaseFileName", ref errors);
                ValidateStringForNull(entity.ImportSettings.FileLocation, "ListItemImportSettings.ImportSettings.FileLocation", ref errors);
            }           
            
            // Have we added errors ?
            return errors.Count == currentCount;
        }


        #endregion Methods
    }
}